﻿using System;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace Framework.Utils
{
    public static class MonoExtends
    {
        public static string GetListString<T>(this List<T> self)
        {
            if (self == null)
                return "null";
            
            StringBuilder sb = new StringBuilder();
            foreach (var t in self)
            {
                sb.Append(t).Append(",");
            }

            return sb.ToString();
        }
        public static T AddScriptComponent<T>(this GameObject self) where T:Component
        {
            T com = self.GetComponent<T>();
            if (com != null)
                return com;
            
            return self.AddComponent<T>();
        }

        public static Component AddScriptComponent(this Transform self, Type type)
        {
            Component com = self.GetComponent(type);
            if (com != null)
                return com;

            return self.gameObject.AddComponent(type);
        }
        
        public static Component AddScriptComponent(this GameObject self, Type type)
        {
            Component com = self.GetComponent(type);
            if (com != null)
                return com;

            return self.AddComponent(type);
        }
        
        public static void RemoveChildren(this Transform self)
        {
            for (int i = self.childCount - 1; i >= 0; i--)
            {
                Object.Destroy(self.GetChild(i).gameObject);
            }
        }

        public static void Show(this Transform transform)
        {
            transform.gameObject.SetActive(true);
        }
        
        public static void Hide(this Transform transform)
        {
            transform.gameObject.SetActive(false);
        }
        public static Text GetText(this Transform transform, string path = null)
        {
            if (path == null)
            {
                return transform.GetComponent<Text>();
            }

            Transform obj = transform.Find(path);
            return obj.GetComponent<Text>();
        }

        public static Button GetButton(this Transform transform, string path = null)
        {
            if (path == null)
            {
                return transform.GetComponent<Button>();
            }

            return transform.Find(path).GetComponent<Button>();
        }

        public static RectTransform GetRectTransform(this Transform transform, string path = null)
        {
            if (path == null)
            {
                return transform.GetComponent<RectTransform>();
            }

            return transform.Find(path).GetComponent<RectTransform>();
        }

        public static RawImage GetRawImage(this Transform transform, string path = null)
        {
            if (path == null)
            {
                return transform.GetComponent<RawImage>();
            }

            return transform.Find(path).GetComponent<RawImage>();
        }

        public static Image GetImage(this Transform transform, string path = null)
        {
            if (path == null)
            {
                return transform.GetComponent<Image>();
            }

            return transform.Find(path).GetComponent<Image>();
        }
        
        public static SpriteRenderer GetSpriteRender(this Transform transform, string path = null)
        {
            if (path == null)
            {
                return transform.GetComponent<SpriteRenderer>();
            }

            SpriteRenderer spriteRenderer = transform.Find(path).GetComponent<SpriteRenderer>();
            return spriteRenderer;
        }
        
        public static MeshRenderer GetMeshRender(this Transform transform, string path = null)
        {
            if (path == null)
            {
                return transform.GetComponent<MeshRenderer>();
            }

            MeshRenderer meshRenderer = transform.Find(path).GetComponent<MeshRenderer>();
            return meshRenderer;
        }

        public static void SetChildrenSortingOrder(this Transform transform, int sortingOrder)
        {
            MeshRenderer[] coms = transform.GetComponentsInChildren<MeshRenderer>();
            foreach (MeshRenderer meshRenderer in coms)
            {
                meshRenderer.sortingOrder = sortingOrder;
            }

            SpriteRenderer[] sprits = transform.GetComponentsInChildren<SpriteRenderer>();
            foreach (SpriteRenderer sprite in sprits)
            {
                sprite.sortingOrder = sortingOrder;
            }
        }
        
        public static Empty4Raycast GetEmpty4Raycast(this Transform transform, string path = null)
        {
            if (path == null)
            {
                return transform.GetComponent<Empty4Raycast>();
            }

            Empty4Raycast empty4Raycast = transform.Find(path).GetComponent<Empty4Raycast>();
            return empty4Raycast;
        }

        public static Toggle GetToggle(this Transform transform, string path)
        {
            return transform.Find(path).GetComponent<Toggle>();
        }

        public static Slider GetSlider(this Transform transform, string path)
        {
            return transform.Find(path).GetComponent<Slider>();
        }
        
        public static TextMeshProUGUI GetTextMeshPro(this Transform transform, string path)
        {
            return transform.Find(path).GetComponent<TextMeshProUGUI>();
        }

        public static void SetChildrenLayer(this GameObject obj, int layer)
        {
            int oldLayer = obj.layer;
            obj.layer = layer;
            foreach (var child in obj.GetComponentsInChildren<Transform>())
            {
                //只有之前跟父节点相同图层的物体才需要改图层
                if (child.gameObject.layer == oldLayer)
                    child.gameObject.layer = layer;
            }
        }
    }
}